package com.zzl.springboot.utils;


import java.io.Serializable;

/**
 * 返回值DataResult
 */
public class DataResult implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 请求响应code，0为成功 其他为失败
     */
    private Integer code;

    /**
     * 响应异常码详细信息
     */
    private String msg;

    /**
     * 保存数据
     */
    private Object data;

    private DataResult() {}

    private DataResult(Integer code, String msg) {
        this.code = code;
        this.data = null;
        this.msg = msg;
    }

    private DataResult(Integer code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    /**
     * 操作成功 不带数据
     */
    public static DataResult success() {
        return new DataResult(0, "操作成功");
    }

    /**
     * 操作成功 带数据, 带自定义消息
     */
    public static DataResult success(Object data, String msg) {
        if(msg == null) msg = "操作成功";
        return new DataResult(0, data, msg);
    }

    /**
     * 操作失败 不带数据
     */
    public static DataResult fail() {
        return new DataResult(1, "操作失败");
    }

    /**
     * 操作失败 不带数据，自定义消息
     */
    public static DataResult fail(String msg) {
        return new DataResult(1, msg);
    }


    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", code=").append(code);
        sb.append(", msg=").append(msg);
        sb.append(", data=").append(data);
        sb.append("]");
        return sb.toString();
    }
}
